package com.aj.http.response;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.aj.databases.entry.Family;
import com.aj.databases.entry.Home;

public class HomeResponse extends BaseRespond {

	private List<Home> homeList;

	public HomeResponse(JSONObject json) {
		super(json);
		if (common != null && common.getResult() != null) {
			try {
				if (common.getResult().has("familyList")) {
					JSONArray jsonArray = new JSONArray(common.getResult()
							.getString("familyList"));
					if (jsonArray != null) {
						homeList = new ArrayList<Home>();

						for (int i = 0; i < jsonArray.length(); i++) {
							JSONObject tempJSONObject = jsonArray
									.getJSONObject(i);
							if (tempJSONObject.has("familyId")) {
								Home home = new Home();
								home.setFamilyId(tempJSONObject
										.getString("familyId"));
								if (tempJSONObject.has("userList")) {
									Collection<Family> familyList = new ArrayList<Family>();
									JSONArray userListJSONArray = new JSONArray(
											tempJSONObject
													.getString("userList"));
									for (int j = 0; j < userListJSONArray
											.length(); j++) {
										Family family = new Family();
										JSONObject familyJSONObject = userListJSONArray
												.getJSONObject(j);
										if (familyJSONObject.has("userId")) {
											family.setUserid(familyJSONObject
													.optString("userId"));
										}
										if (familyJSONObject.has("callId")) {
											family.setCallId(familyJSONObject
													.optString("callId"));
										}
										if (familyJSONObject.has("callName")) {
											family.setCallName(familyJSONObject
													.optString("callName"));
										}
										if (familyJSONObject.has("photo")) {
											family.setPhoto_url(familyJSONObject
													.optString("photo"));
										}
										if (familyJSONObject.has("isMarried")) {
											family.setIsMarried(familyJSONObject
													.optInt("isMarried"));
										}
										if (familyJSONObject.has("age")) {
											family.setAge(familyJSONObject
													.optInt("age"));
										}
										family.setHome(home);
										familyList.add(family);
									}
									home.setFamilyList(familyList);
								}
								homeList.add(home);
							}
						}
					}
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
	}

	public List<Home> getHomeList() {
		return homeList;
	}

	public void setHomeList(List<Home> homeList) {
		this.homeList = homeList;
	}

}
